<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Collector Control</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }

        .container {
            background: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        h1 {
            color: #333;
            margin-bottom: 2rem;
        }

        button {
            padding: 12px 24px;
            margin: 0 10px;
            font-size: 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .linkup {
            background-color: #4CAF50;
            color: white;
        }

        .linkup:hover {
            background-color: #45a049;
        }

        .linkdown {
            background-color: #f44336;
            color: white;
        }

        .linkdown:hover {
            background-color: #da190b;
        }

        #status {
            margin-top: 1rem;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }

        .info {
            background-color: #e2e3e5;
            color: #383d41;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Collector Control Panel</h1>
        <p style="color: #666; margin-bottom: 2rem;">通过服务器端发送命令</p>
        <button class="linkup" onclick="sendCommand('linkup')">Link Up</button>
        <button class="linkdown" onclick="sendCommand('linkdown')">Link Down</button>
        <div id="status"></div>
    </div>

    <script>
        async function sendCommand(command) {
            const statusDiv = document.getElementById('status');
            statusDiv.style.display = 'block';
            statusDiv.textContent = `通过服务器发送 ${command} 命令中...`;
            statusDiv.className = 'info';

            try {
                // 现在调用本地的playground backend API，而不是直接调用collector
                const response = await fetch('/playground/api/command', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ cmd: command }),
                });

                const result = await response.json();

                if (response.ok) {
                    statusDiv.textContent = `服务器端 ${command} 命令发送成功！`;
                    statusDiv.className = 'success';
                } else {
                    statusDiv.textContent = `错误: ${result.detail || response.statusText}`;
                    statusDiv.className = 'error';
                }
            } catch (error) {
                statusDiv.textContent = `网络错误: ${error.message}`;
                statusDiv.className = 'error';
            }
        }
    </script>
</body>

</html>
